<?php

require_once CORE_DIRECTORY.OS_PATH_SEPARATOR.'basePlugin.php';

/*
 * BaseMetadata is the parent class for metadata parsing plugins
 * it handles one file
 */
class BaseMetadata extends BasePlugin
{
	protected $file;
	private $metadata;

	// Save the metadata into the database, allow access to the private baseMetadata::DBSave() method
	public function saveMeta()
	{
		return $this->DBSave();
	}
	
	// delete a specific metadata from the database
	public static function deleteMeta($type, $id)
	{
		global $DB;
		$type = (int)$type;
		$id = (int)$id;
		
		$DB->queryExec(
			debug(
				'DELETE FROM '.META_TABLE.' WHERE id='.$id.' AND type='.$type.';', 
				'Delete a meta',
				JGLIB_DEBUG_SQLQ
			)
		);
		$DB->queryExec(
			debug(
				'DELETE FROM '.ITEM_META_TABLE.' WHERE id_meta='.$id.';',
				'Delete a meta',
				JGLIB_DEBUG_SQLQ
			)
		);
		return;
		//TODO
	}
	
	// Add a metadata to the list of actual metadata
	// Do some checking before see config.php
	public function addMetadata($metadata, $type)
	{
		$type = (int)$type;
		$metadata = trim($metadata);
		
		global $METADATA_BLACKLIST;
		if(
			strlen($metadata) >= MINIMUM_METADATA_LENGTH
			&& (!NO_NUMERIC_TAGS || !is_numeric($metadata))
			&& !in_array($metadata, $METADATA_BLACKLIST)
		)
		{
			$this->metadata[$type][] = $metadata;
		}
		
		foreach($this->metadata as &$a)
		{
			$a = array_unique($a);
		}
	}
	
	// Create a new instance of a plugin with the file to handle
	public function __construct($file)
	{
		if(is_object($file))
		{
			$this->file = $file;
		}
		else
		{
			throw new Exception('Passed parameter have to be a File object');
		}
		$this->metadata = array();
	}
	
	// Save each metadata into the database (add if new, increment counter if exist)
	private function DBSave()
	{
		if(isForeachable($this->metadata))
		{
			global $DB;
			global $TYPE_LIST;
			
			foreach($TYPE_LIST as $type) // 2D array foreach (I didn't put all the brackets, it will be unreadable)
			if(isset($this->metadata[$type]) && isForeachable($this->metadata[$type]))
			foreach($this->metadata[$type] as $meta)
			{
				$result = $DB->arrayQuery(
					debug('
						SELECT id, counter FROM '.META_TABLE.' WHERE value="'.$meta.'" and type='.$type.' LIMIT 1;',
						'SQL',
						JGLIB_DEBUG_NONE
					)
				);
				
				if(empty($result))
				{
					$DB->queryExec(
						debug('
							INSERT INTO '.META_TABLE.' (id, value, type, counter) 
							VALUES('.$DB->getNextId('meta').',"'.$DB->escapeString($meta).'", '.(int)$type.', 1);',
							'SQL',
							JGLIB_DEBUG_SQLQ
						)
					);
					$lastId = $DB->getLastId('meta');
				}
				else
				{
					$lastId  = $result[0]['id'];
					$counter = $result[0]['counter'];
					$DB->queryExec(
						debug('
							UPDATE '.META_TABLE.' SET counter='.(int)($counter+1).' WHERE value="'.$DB->escapeString($meta).'" AND type='.(int)$type.';',
							'SQL',
							JGLIB_DEBUG_SQLQ
						)
					);
				}
				
				$DB->queryExec(
					debug('
						INSERT INTO '.ITEM_META_TABLE.' (id_item, id_meta) VALUES('.(int)$this->file->getId().', '.(int)$lastId.')',
						'SQL',
						JGLIB_DEBUG_SQLQ
					)
				);
			}
		}
		
		return $this->metadata;
	}
	
	// Delete the specific metadata from the database
	private function DBDelete()
	{
		//TODO
	}
}
